//获取应用实例
var app = getApp()
var util = require('../../../utils/util.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    height:500,
    recommend_pic:""
  },

  /**
   * 生命周期函数--监听页面加载
   * pType 1白银2黄金版 3钻石版4白金版 5黑金版 6钻石plus 7旗舰版
   */
  onLoad: function (options) {
    var that=this;
    // 获取系统信息
    wx.getSystemInfo({
      success: function (res) {
        // 获取可使用窗口宽度
        let clientHeight = res.windowHeight-res.statusBarHeight;
        // 获取可使用窗口高度
        let clientWidth = res.windowWidth;
        // 算出比例
        let ratio = 750 / clientWidth;
        // 算出高度(单位rpx)
        let height = clientHeight * ratio;
        // 设置高度
        that.setData({
          height: height-100
        });
      }
    })
    this.getUserInfo();
    // 判断类型
    this.setData(options)
    this.getCode(options.pType);
  },
  // 获取用户信息
  getUserInfo(){
    let userinfo = wx.getStorageSync('customer_id');
    this.setData({
      userinfo:userinfo
    })
  },
  // 获取店铺推荐二维码
  getCode(){
    var that = this;
    wx.request({
      url: app.globalData.urls + '/order-manage/get-qrcode',
      header: app.getRequestHeader(),
      method:"POST",
      data: {
        bdmin_user_id:util.getBdmin_user_id(),
        type:this.data.pType
      },
      success: function (res) {
        if(res.data.code==200){
          console.log('--保存到相册图片--',res.data.data.recommend_pic)
          that.setData({
            recommend_pic:res.data.data.recommend_pic
          })
        }
      }
    })
  },
  // 保存到相册
  savephoto(){
    wx.showLoading({
      title: '保存中...', 
      mask: true,
    });
    
    var that=this;
    wx.downloadFile({
      url:that.data.recommend_pic,
      success: function(res) {
        console.log("成功还是失败",res)
        if (res.statusCode === 200) {
          let img = res.tempFilePath;
          wx.saveImageToPhotosAlbum({
            filePath: img,
            success(res) {
              wx.showToast({
                title: '保存成功',
                icon: 'success',
                duration: 2000
              });
            },
            fail(res) {
              wx.showToast({
                title: '保存失败',
                icon: 'error',
                duration: 2000
              });
            }
          });
        }
      }
    });
  },
  // 进入到店铺 
  goshop(){
    wx.navigateTo({
      url: '/pages/business/index/index',
    })
  },

})